package com.ys.controller.web.account;

import java.math.BigDecimal;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ys.service.account.TcwNeedPayAccountPayService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.ys.config.MenuId;
import com.ys.dto.SubGridConfigDto;
import com.ys.entity.account.TcwNeedPayAccountPay;
import com.ys.entity.account.TcwNeedPayAccountPayItem;
import com.ys.service.sys.CommonApiService;
import com.ys.vo.W;

@Controller
@RequestMapping("/web/account/tcwNeedPayAccountPay")
public class WebTcwNeedPayAccountPayController {
	
	@Autowired
	private CommonApiService<TcwNeedPayAccountPay> commonApiService;


	@Autowired
	private TcwNeedPayAccountPayService tcwNeedPayAccountPayService;

	/*
	 * 新增
	 */
	@GetMapping("/toAdd")
	public String toAdd(Model model, HttpServletRequest request, HttpServletResponse response){

		try {
			String bid = commonApiService.getCommonNextBid(MenuId.TCW_NeedPayAccountPay);
			
			TcwNeedPayAccountPay entity = new TcwNeedPayAccountPay()
					.setBid(bid)
					.setCreateTime(new Date());

			Map<String, Object> extraParam = new HashMap<>();
			extraParam.put("status", 1);
			extraParam.put("payStatus", 0);
			
			// 子列表配置信息
			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TCW_NeedPayAccountPay, new ArrayList<Map<String, Object>>());
			
			return commonApiService.toAdd(model, entity, MenuId.TCW_NeedPayAccountPay, extraParam, subGridConfig);

		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, e.getMessage());
		}
	}
	
	/*
	 * 编辑
	 */
	@RequiresPermissions("tcwNeedPayAccountPay:add")
	@GetMapping("/toFlow")
	public String toFlow(Model model, HttpServletRequest request, HttpServletResponse response,
			String bid){
		try {
			if (bid == null || bid.length() == 0) {
				return W.error(model, "单号不能为空");
			}

			TcwNeedPayAccountPay entity = new TcwNeedPayAccountPay().setBid(bid).queryFirst();

			Map<String, Object> extraParam = tcwNeedPayAccountPayService.getExtraParam(bid);

			// 子列表配置信息
			List<Map<String, Object>> sonlist = tcwNeedPayAccountPayService.getItemList(bid);

			SubGridConfigDto subGridConfig = commonApiService.getSubGrid(MenuId.TCW_NeedPayAccountPay, sonlist);
			return commonApiService.toEdit(model, entity.getId(), new TcwNeedPayAccountPay(), MenuId.TCW_NeedPayAccountPay, extraParam, subGridConfig);

		} catch (Exception e) {
			e.printStackTrace();
			return W.error(model, "数据有误，请联系客服");
		}
	}
}